package com.xmut.mall.member.service.impl;

import com.xmut.mall.member.utils.RespBean;
import com.xmut.mall.member.vo.CollectProductVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.member.utils.PageUtils;
import com.xmut.mall.member.utils.Query;

import com.xmut.mall.member.dao.MemberCollectSkuDao;
import com.xmut.mall.member.entity.MemberCollectSkuEntity;
import com.xmut.mall.member.service.MemberCollectSkuService;


@Service("memberCollectSpuService")
public class MemberCollectSkuServiceImpl extends ServiceImpl<MemberCollectSkuDao, MemberCollectSkuEntity> implements MemberCollectSkuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberCollectSkuEntity> page = this.page(
                new Query<MemberCollectSkuEntity>().getPage(params),
                new QueryWrapper<MemberCollectSkuEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据用户Id获取该用户收藏的商品
     * @param userId
     * @return
     */
    @Cacheable(value = "UserCollectionSpu",key = "#root.methodName+#root.args[0]")
    @Override
    public List<MemberCollectSkuEntity> getCollectionSpuByUserId(Long userId) {
        return this.list(new QueryWrapper<MemberCollectSkuEntity>().eq("member_id",userId));
    }

    /**
     * 检查用户是否收藏了该商品
     * @param userId
     * @param skuId
     * @return
     */
    @Override
    public Boolean isColletcion(Long userId, Long skuId) {
        MemberCollectSkuEntity memberCollectSku = this.getOne(new QueryWrapper<MemberCollectSkuEntity>()
                .eq("member_id", userId)
                .eq("sku_id", skuId));
        if(memberCollectSku!=null){
            //查到东西不是null就说明收藏了
            return true;
        }else {
            // 如果是null就没收藏
            return false;
        }
    }

    /**
     * 用户取消收藏商品
     * @param userId
     * @param skuId
     * @return
     */
    @CacheEvict(value = "UserCollection",allEntries = true)
    @Override
    public Boolean cancelCollection(Long userId, Long skuId) {
        return  this.remove(new QueryWrapper<MemberCollectSkuEntity>()
                .eq("member_id", userId)
                .eq("sku_id", skuId));

    }

    /**
     * 用户收藏商品
     * @return
     */
    @CacheEvict(value = "UserCollection",allEntries = true)
    @Override
    public RespBean collectProduct(CollectProductVo vo) {
        // 先查一下是否收藏了该商品 防止重复网数据库写一样的数据
        int count = this.count(new QueryWrapper<MemberCollectSkuEntity>()
                .eq("member_id", vo.getMemberId())
                .eq("sku_id", vo.getSkuId()));
        if(count>0){
            return RespBean.error("请勿重复收藏商品");
        }
        // 如果没有收藏 现在开始往数据库写数据
        MemberCollectSkuEntity collectSkuEntity = new MemberCollectSkuEntity();
        collectSkuEntity.setSkuId(vo.getSkuId());
        collectSkuEntity.setSpuId(vo.getSpuId());
        collectSkuEntity.setMemberId(vo.getMemberId());
        collectSkuEntity.setSkuName(vo.getSkuName());
        collectSkuEntity.setSkuPrice(vo.getSkuPrice());
        collectSkuEntity.setSkuImg(vo.getSkuImg());
        collectSkuEntity.setBrandName(vo.getBrandName());
        collectSkuEntity.setCatalogId(vo.getCatalogId());
        collectSkuEntity.setCreateTime(new Date());
        collectSkuEntity.setStoreId(vo.getStoreId());
        collectSkuEntity.setStoreName(vo.getStoreName());
        boolean save = this.save(collectSkuEntity);
        if(save){
            return RespBean.success("收藏商品成功");
        }else {
            return RespBean.error("收藏商品失败,请重试");
        }

    }

    /**
     * 获取用户收藏商品的分页数据
     * @param params
     * @return
     */
    @Cacheable(value = "UserCollection",key = "#root.methodName+#root.args[0]")
    @Override
    public RespBean getUserCollectionByPage(Map<String, Object> params) {
        QueryWrapper<MemberCollectSkuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("member_id",params.get("userId"));
        IPage<MemberCollectSkuEntity> page = this.page(
                new Query<MemberCollectSkuEntity>().getPage(params),queryWrapper
        );
        // 设置total的值
        if(page.getRecords()!=null){
            page.setTotal(page.getRecords().size());
        }
        return RespBean.success("获取成功",page);
    }

}